import wmi
import time
import xlwings


def fill_excel_report_head_info():
    try:
        app = xlwings.App(visible=False, add_book=False)  # 程序不可见False，只打开不新建工作薄False
        app.display_alerts = False  # 警告关闭
        app.screen_updating = False  # 屏幕更新关闭
        xlsx_path = "MOPC-XXX_Virtual-Physical DeviceTestResult.xlsx"
        workbook = app.books.open(xlsx_path)

        # sheet = workbook.sheets.active
        # 获取单个单元格的值
        # C2 = sheet.range('C2').value
        # print(C2)

        # 连接到指定单元格.# 写入产品名字和版本号
        data = workbook.sheets('Sheet1').range('C1')
        driver_name = input("input Driver name and version:")
        data.value = driver_name
        # 写入产品12NC
        data = workbook.sheets('Sheet1').range('C2')
        driver_12NC = input("input Driver 12NC:")
        data.value = driver_12NC
        # 写入测试员名字
        data = workbook.sheets('Sheet1').range('C3')
        tester_name = input("input tester name:")
        data.value = tester_name
        # 写入测试日期，格式：年月日
        data = workbook.sheets('Sheet1').range('C4')
        date = time.strftime("%Y-%m-%d", time.localtime())
        data.value = date
        # 写入MultiOne 版本
        data = workbook.sheets('Sheet1').range('C5')
        multione_version = input("input Multione version:")
        data.value = multione_version
    except Exception as e:
        print("Write excel report occur exception！")
        workbook.close()  # 关闭文件
        app.quit()  # 关闭程序
        time.sleep(5)
    else:  # try中没有引发异常则执行else语句
        print("Write excel report not have exception, 保存退出！")
        workbook.save()  # 保存文件
        workbook.close()  # 关闭文件
        app.quit()  # 关闭程序
        time.sleep(5)
    finally:
        print("write excel report finish.")
        # for process in wmi.WMI().Win32_Process():  # 遍历所有进程，时间要好几秒
        #     # print(process.ProcessId, process.Name)
        #     # process_list.append(process.Name)   # 将所有进程写入列表
        #     # with open("process_list.txt", "w") as f:  # 将进程列表写入文件。每次写入会覆盖原有内容
        #     #     f.writelines(process_list)
        #     if (process.Name == "EXCEL.EXE"):
        #         process.Terminate()  # 遍历所有进程，如果有MO进程则终止进程


if __name__ == "__main__":
    fill_excel_report_head_info()

